package com.screen.screendisplay.service.impl.al;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.screen.screendisplay.entity.al.AlProdoperStationPer;
import com.screen.screendisplay.mapper.al.AlProdoperStationPerMapper;
import com.screen.screendisplay.service.al.AlProdoperStationPerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.screen.screendisplay.vo.al.AlProdoperStationPerVO;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 电厂火电占比表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class AlProdoperStationPerServiceImpl extends ServiceImpl<AlProdoperStationPerMapper, AlProdoperStationPer> implements AlProdoperStationPerService {

    @Override
    public List<AlProdoperStationPerVO> getPowerStationRatio() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        LocalDate startDate = currentDate.minusMonths(11);
        
        // 查询最近12个月的数据
        QueryWrapper<AlProdoperStationPer> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("year", startDate.getYear())
                .orderByAsc("year", "month", "day");
        
        List<AlProdoperStationPer> dataList = this.list(queryWrapper);
        
        // 按年月分组，取每月最后一天的数据
        Map<String, AlProdoperStationPer> monthlyMaxData = new HashMap<>();
        for (AlProdoperStationPer data : dataList) {
            // 过滤出最近12个月的数据
            LocalDate dataDate = LocalDate.of(data.getYear(), data.getMonth(), 1);
            if (!dataDate.isBefore(startDate)) {
                String key = data.getYear() + "-" + data.getMonth();
                // 如果该月还没有数据，或者当前数据的day更大，则更新
                if (!monthlyMaxData.containsKey(key) || 
                    monthlyMaxData.get(key).getDay() < data.getDay()) {
                    monthlyMaxData.put(key, data);
                }
            }
        }
        
        // 转换为VO并按年月排序
        List<AlProdoperStationPerVO> result = monthlyMaxData.values().stream()
                .sorted(Comparator.comparing(AlProdoperStationPer::getYear)
                        .thenComparing(AlProdoperStationPer::getMonth))
                .map(data -> {
                    AlProdoperStationPerVO vo = new AlProdoperStationPerVO();
                    vo.setYear(data.getYear());
                    vo.setMonth(data.getMonth());
                    vo.setHuolinheGreenPower(data.getHuolinheGreenPower());
                    vo.setBaiyinhuaGreenPower(data.getBaiyinhuaGreenPower());
                    vo.setHuolinheFossilPower(data.getHuolinheFossilPower());
                    vo.setBaiyinhuaFossilPower(data.getBaiyinhuaFossilPower());
                    vo.setHuolinheGreenRatio(data.getHuolinheGreenRatio());
                    vo.setBaiyinhuaGreenRatio(data.getBaiyinhuaGreenRatio());
                    return vo;
                })
                .collect(Collectors.toList());
        
        return result;
    }
}
